package com.zq.assistant.ui.file.adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zq.assistant.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zhangqiang on 17-6-19.
 */

public class FileDownloadAdapter extends CursorAdapter {

    public FileDownloadAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_download_info,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.fileName.setText(cursor.getString(cursor.getColumnIndex("url")));
        int contentLength = cursor.getInt(cursor.getColumnIndex("contentLength"));
        int currentLength = cursor.getInt(cursor.getColumnIndex("currentLength"));
        viewHolder.progressBar.setProgress((int) ((float)currentLength/contentLength*100));
    }

    static class ViewHolder {

        @InjectView(R.id.file_name)
        TextView fileName;
        @InjectView(R.id.progressBar)
        ProgressBar progressBar;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
